package com.example.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sys.component.AbstractPasswordEncoder;
import com.example.sys.dao.SystemUserMapper;
import com.example.sys.dto.SystemUserDTO;
import com.example.sys.entity.SystemUser;
import com.example.sys.service.ISystemUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户信息表 服务实现类
 * </p>
 *
 * @author ywh
 * @since 2024-08-13
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> implements ISystemUserService {

    private static final String DEF_PASSWORD = "123456";

    private final AbstractPasswordEncoder passwordEncoder;

    @Autowired
    public SystemUserServiceImpl(AbstractPasswordEncoder passwordEncoder){
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public boolean saveUserDTO(SystemUserDTO dto) {
        SystemUser save = new SystemUser();
        BeanUtils.copyProperties(dto, save);
        //密码加密
        if (StringUtils.isBlank(save.getLoginPassword())){
            save.setLoginPassword(DEF_PASSWORD);
        }
        save.setLoginPassword(passwordEncoder.encode(save.getLoginPassword()));
        return this.save(save);
    }
}
